<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>作业</title>
</head>

<body>
    <script>
        //1.封装一个函数实现字符串的翻转 比如 foo('哦泡')->泡哦
        function strReverse(str) {
            var newstr = '';
            for(var i = str.length-1; i >= 0; i--){
                newstr = newstr.concat(str.charAt(i));
            }
            return newstr;
        }
        console.log(strReverse("abcd"));

        //2.字符串 digite = "0123456789", 把前 5 个字符分割出来，返回单个字符的数组, 注意， 返回数组里面的数字是字符串类型, string.split("")
        var digite = "0123456789";
        // var newDigite = digite.split(5);
        for(var str of digite.split(5)) {
            // console.log(str);
            var newstr = [];
            for(var i = 0; i < str.length; i++) {
            // for(var s of str){
                // console.log(str[i]);
                // console.log(s);
                newstr[newstr.length] = str[i];
                // newstr[newstr.length] = s;
            }
            // console.log(newstr);
        }

        //3.已知有字符串 foo=”get-element-by-id”,写一个 function 将其转化成驼峰表示法”getElementById”.
        var foo = "get-element-by-id";
        function toHumpCase(str) {
            var arr = str.split("-");
            var newStr = "";
            for(var i = 0; i < arr.length; i++) {
                if(i === 0) {
                    newStr = newStr.concat(arr[i]);
                    continue;
                }else {
                    newStr = newStr.concat(arr[i].replace(arr[i][0],arr[i].charAt(0).toUpperCase()));
                }  
            }
            return newStr;
        }
        console.log(toHumpCase(foo));
        // toHumpCase(foo);

        //4.定义一个函数,将"今天是什么鬼天气，这么热，明天要去游泳！"里面的有"天"的下标输出(用 indexof())
        var p = "今天是什么鬼天气，这么热，明天要去游泳！";
        var char = "天";
        function searchCharIndex(str,char) {
            for(var i = 0; i < str.length; i++) {
                if(str.charAt(i) === char) {
                    console.log(i);
                }
            }
        }
        searchCharIndex(p,char);
        // console.log(searchCharIndex(p,char));
        
        //定义一个函数,将"今天是什么鬼天气，这么热，明天要去游泳！"里面的有"天"替换成"日"(用 replace())
        var p1 = "今天是什么鬼天气，这么热，明天要去游泳！";
        var char1 = "天";
        var newChar = "日";
        function searchCharIndex1(str,char,newChar) {
            for(var i = 0; i < str.length; i++) {
                if(str.charAt(i) === char) {
                    str = str.replace(char,newChar);
                }
            }
            return str;
        }
        console.log(searchCharIndex1(p1,char1,newChar));

        //5. 查找字符串中(abbcccddddd)出现最多的字符和个数 出现最多的是d出现了5次
        var str = "agfhfahdfdffdsdsfwe";
        var newstr = {};
        for(var i = 0; i < str.length; i++) {
            newstr[str[i]] ? (newstr[str[i]] += 1) : (newstr[str[i]] = 1);
        }
        console.log(newstr);
        var max = 0;
        var char = "";
        for(var k in newstr){
            if(newstr[k] > max){
                max = newstr[k];
                char = k;
            }
            // console.log(newstr[k]);
            // newstr[k] > max ? (max = newstr[k];char = k) : (max = 0);
        }
        console.log(max);
        console.log("出现次数最多的字符为："+ char, "个数为："+ max);

        /* var str = "bacbcddcddd";
        var strArray = [];
        for(var i = 0; i<str.length; i++){
            if(str[i] !== str[i+1]){
                strArray[strArray.length] = [i, i+1];
            }
        }
        // console.log(strArray);
        var newkey = [];
        for(var j=0;j<strArray.length;j++){
            newkey[newkey.length] = (strArray[j])[1];
        }
        console.log(newkey);
        var max = 0;
        for(var i=0;i<newkey.length;i++) {
            if(newkey[i+1]- newkey[i] > max) {
                max = newkey[i+1]- newkey[i];
            }
        }
        console.log(max);
        var key = newkey[newkey.length-1] - 1;
        console.log("出现次数最多的字符为："+ str[key],"个数为："+ max); */

        /* var str = "abbcccddddd";
        var strObj = {};
        var strKey = '';
        var strValue = 0;
        var max = 0;
        for(var i = 0;i<=str.length;i++){
				if(str[i] != str[i+1]){
					strObj[str[i]] = i+1
				}
			}
		for(var key in strObj){
			if(max<strObj[key]-max){
				max = strObj[key]-strValue
				strValue = strObj[key]
				strKey = key
			}
		}
		console.log('最大为'+strKey+'个数为'+max) */
    </script>
</body>

</html>